#include "VertexBuffer.h"

namespace devii
{

VertexBuffer::VertexBuffer(ID3D11Device * device, ID3D11DeviceContext* context) : Buffer(device, context)
{
}

VertexBuffer::~VertexBuffer()
{
	for(unsigned int i = 0; i < buffers.size(); i++)
	{
		buffers[i]->Release();
	}
}


void VertexBuffer::pushBuffer(const void * const data, const unsigned int nr_of_vertices, const unsigned int size, const unsigned int offset, const bool changeable)
{
	ID3D11Buffer * buffer = NULL;

	D3D11_BUFFER_DESC bd;
	HRESULT hr; 
	if(changeable)
	{
		bd.Usage =			D3D11_USAGE_DYNAMIC;
		bd.ByteWidth =		size*nr_of_vertices;
		bd.BindFlags =		D3D11_BIND_VERTEX_BUFFER;
		bd.CPUAccessFlags =	D3D11_CPU_ACCESS_WRITE;
		bd.MiscFlags =		0;
		D3D11_SUBRESOURCE_DATA res_dat;
		res_dat.pSysMem =	data;
		hr = device->CreateBuffer(&bd, &res_dat, &buffer); 
	}
	else
	{
		bd.Usage =			D3D11_USAGE_DEFAULT;
		bd.ByteWidth =		size*nr_of_vertices;
		bd.BindFlags =		D3D11_BIND_VERTEX_BUFFER;
		bd.CPUAccessFlags = 0;
		bd.MiscFlags =		0;
		D3D11_SUBRESOURCE_DATA res_dat;
		res_dat.pSysMem =	data;
		hr = device->CreateBuffer(&bd, &res_dat, &buffer); 
	}

	if(SUCCEEDED(hr))
	{
		buffers.push_back(buffer);
		sizes.push_back(size);
		offsets.push_back(offset);
	}
}

ID3D11Buffer ** VertexBuffer::getBuffer()
{
	return &buffers[0];
}

const unsigned int VertexBuffer::getSize() const
{
	return buffers.size();
}

const unsigned int* VertexBuffer::getStrides() const
{
	return &sizes[0];
}

const unsigned int* VertexBuffer::getOffsets() const
{
	return &offsets[0];
}

void VertexBuffer::setInputLayout(ID3D11InputLayout* const input_layout)
{
	this->input_layout = input_layout;
}

ID3D11InputLayout* VertexBuffer::getInputLayout() const
{
	return input_layout;
}

void VertexBuffer::setPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY topology)
{
	this->topology = topology;
}

const D3D11_PRIMITIVE_TOPOLOGY VertexBuffer::getTopology() const
{
	return topology;
}

}